#include "serialcom.h"

RectangleCoordinateDef g_rectangle_coordinate;
SingleCoordinateDef g_red_coordinate,g_green_coordinate;
SingleCoordinateDef g_origin_coordinate,g_topright_coordinate,g_topleft_coordinate,g_bottomright_coordinate,g_bottomleft_coordinate;
SinglePWMCoorDef g_red_pwmcoor,g_green_pwmcoor;
int one_pointx;
int one_pointy;
uint16_t theNum=1;
unsigned char g_raspi_mood,set;
PointDef g_rectangle[512];
PointDef g_rectangle_buff[512];

RxProBufDef RxProBuf;

void serialInit(void)
{
	__HAL_UART_ENABLE_IT(&huart2,UART_IT_IDLE);
	Uart_RxAgain(&huart2);
}



//数据发送函数
void sendInt(int data){

	 char send[8]; // 数字转换后的字符串
    sprintf(send, "%d", data); // 将数字转换为字符串
	 HAL_UART_Transmit(&huart2, " ", 1,200 );
	 HAL_UART_Transmit(&huart2, send, strlen(send),200 );
	 HAL_UART_Transmit(&huart2, " ", 1,200 );
}

//数值解析函数，传入初始位置和结束符，获取数值
uint16_t Numerical_analysis(uint8_t *NumStartIndex,char endchar,uint8_t **header)
{
	uint16_t value = 0;

	while(1)
	{
		if (*NumStartIndex==endchar)break;

		value *=10;
		value += (*NumStartIndex-'0');
		NumStartIndex++;
	}
	*header = ++NumStartIndex;

	return value;
}

//判断接收的数据是什么模式
uint8_t judgmenMood(RxProBufDef theBuf ){

    uint8_t getRight=0;
    uint8_t theMood=0;
if( theBuf.rxProBuf[0]=='S' && theBuf.rxProBuf[1]=='T' &&theBuf.rxProBuf[2]=='R'   )
{
	  getRight = 1;
	  CO.receiveFlag = 1;
}
if(getRight==1)
{
        if(theBuf.rxProBuf[3]=='R'&&theBuf.rxProBuf[4]=='C'&&theBuf.rxProBuf[5]=='%'){
        	theMood=REDCOORDINATE;//接收红色坐标模式
        }
        if(theBuf.rxProBuf[3]=='G'&&theBuf.rxProBuf[4]=='C'&&theBuf.rxProBuf[5]=='%'){
        	theMood=GREENCOORDINATE;//接收绿色坐标模式
        }
        if(theBuf.rxProBuf[3]=='B'&&theBuf.rxProBuf[4]=='R'&&theBuf.rxProBuf[5]=='%'){
        	theMood=RECTANGLE;//接收矩形框坐标模式
        }
        if(theBuf.rxProBuf[3]=='R'&&theBuf.rxProBuf[4]=='G'&&theBuf.rxProBuf[5]=='%'){
        	theMood=REDANDGREENCOORDINATE;//红绿双坐标接收模式
        }
}
return theMood;
}




//将接收的矩形框路线坐标进行解析 并存储
void rectangleCoordinateGet(uint8_t *rxProBuf,RectangleCoordinateDef rectangleCoordinate )
{
	 theNum=1;

    uint16_t numbuf;
	rectangleCoordinate.coordinateNum=0;


     while(1){
    	 if(*rxProBuf=='-'){
        numbuf=Numerical_analysis(rxProBuf+sizeof(uint8_t),'*',&rxProBuf);
		 theNum++;
		 rectangleCoordinate.coordinateNum=theNum;
   	     rectangleCoordinate.rectangleCoordinateX[theNum]=Numerical_analysis(rxProBuf, '*', &rxProBuf);
   	     rectangleCoordinate.rectangleCoordinateY[theNum]=Numerical_analysis(rxProBuf, '#', &rxProBuf);

   	     g_rectangle[theNum].x=(rectangleCoordinate.rectangleCoordinateX[theNum]-g_origin_coordinate.xCoordinate);
   	     g_rectangle[theNum].y=(g_origin_coordinate.yCoordinate-rectangleCoordinate.rectangleCoordinateY[theNum]);
  	     if(g_rectangle[theNum].x >= 0)
   	     {
   	        g_rectangle[theNum].x = (g_rectangle[theNum].x /( (float)0.5*(g_topright_coordinate.xCoordinate+g_bottomright_coordinate.xCoordinate))) * 0.5*(CO.cornerPoint[0].x+CO.cornerPoint[1].x);
   	     }
   	     else
   	     {
   	    	 g_rectangle[theNum].x = (g_rectangle[theNum].x / ((float)0.5*(g_bottomleft_coordinate.xCoordinate+g_topleft_coordinate.xCoordinate))) * 0.5*(CO.cornerPoint[2].x+CO.cornerPoint[3].x);
   	     }

         if(g_rectangle[theNum].y >= 0)
         {
      	    g_rectangle[theNum].y = (g_rectangle[theNum].y /( (float)0.5*(g_topleft_coordinate.yCoordinate+g_topright_coordinate.yCoordinate))) * 0.5*(CO.cornerPoint[0].y+CO.cornerPoint[3].y);
         }
         else
         {
			g_rectangle[theNum].y = (g_rectangle[theNum].y /((float) 0.5*(g_bottomleft_coordinate.yCoordinate+g_bottomright_coordinate.yCoordinate))) * 0.5*(CO.cornerPoint[1].y+CO.cornerPoint[2].y);
         }
   		 HAL_UART_Transmit(&huart2, "-num=", 5,200 );
   	     sendInt(theNum);
//         g_rectangle_buff[theNum].y=g_rectangle[theNum].y;
//         g_rectangle[theNum].y=g_rectangle[theNum].x;
//         g_rectangle[theNum].x=g_rectangle_buff[theNum].y;
//   		 HAL_UART_Transmit(&huart2, " g_origin_coordinate.x", 22,200 );
//   	     sendInt(g_origin_coordinate.xCoordinate);
//   		 HAL_UART_Transmit(&huart2, " g_origin_coordinate.y", 22,200 );
//   	     sendInt(g_origin_coordinate.yCoordinate);
//   		 HAL_UART_Transmit(&huart2, "/n", 2,200 );
//
//   		 HAL_UART_Transmit(&huart2, " g_topright_coordinate.x", 23,200 );
//   	     sendInt(g_topright_coordinate.xCoordinate);
//   		 HAL_UART_Transmit(&huart2, " g_topright_coordinate.y", 23,200 );
//   	     sendInt(g_topright_coordinate.yCoordinate);
//   		 HAL_UART_Transmit(&huart2, "/n", 2,200 );
//
//   		 HAL_UART_Transmit(&huart2, " num=", 5,200 );
//   	     sendInt(theNum);
//   		 HAL_UART_Transmit(&huart2, " x=", 3,200 );
//   	     sendInt(g_rectangle[theNum].x);
//   		 HAL_UART_Transmit(&huart2, " y=", 3,200 );
//   	     sendInt(g_rectangle[theNum].y);
//   		 HAL_UART_Transmit(&huart2, "/n", 2,200 );
//
//
//   		 HAL_UART_Transmit(&huart2, " CO.cornerPoint[0].x=",21 ,200 );
//   	     sendInt(CO.cornerPoint[0].x);
//   		 HAL_UART_Transmit(&huart2, " CO.cornerPoint[0].y=",21 ,200 );
//   	     sendInt(CO.cornerPoint[0].y);
//   		 HAL_UART_Transmit(&huart2, '/n', 2,200 );
//   		 HAL_UART_Transmit(&huart2, " CO.cornerPoint[2].x=",21 ,200 );
//   	     sendInt(CO.cornerPoint[2].x);
//   		 HAL_UART_Transmit(&huart2, " CO.cornerPoint[2].y=",21 ,200 );
//   	     sendInt(CO.cornerPoint[2].y);
//   		 HAL_UART_Transmit(&huart2, "/n", 2,200 );

   	     return;

    	 }else numbuf=Numerical_analysis(rxProBuf,'*',&rxProBuf);

    		 theNum=numbuf;

			 //分别解析X 与 Y坐标，并将其存储
			 rectangleCoordinate.rectangleCoordinateX[theNum]=Numerical_analysis(rxProBuf, '*', &rxProBuf);
			 rectangleCoordinate.rectangleCoordinateY[theNum]=Numerical_analysis(rxProBuf, '*', &rxProBuf);
//	   	     g_rectangle[theNum].x=rectangleCoordinate.rectangleCoordinateX[theNum];
//	      	 g_rectangle[theNum].y=rectangleCoordinate.rectangleCoordinateY[theNum];
	   	     g_rectangle[theNum].x=(rectangleCoordinate.rectangleCoordinateX[theNum]-g_origin_coordinate.xCoordinate);
	   	     g_rectangle[theNum].y=(g_origin_coordinate.yCoordinate-rectangleCoordinate.rectangleCoordinateY[theNum]);
	  	     if(g_rectangle[theNum].x >= 0)
	   	     {
	   	        g_rectangle[theNum].x = (g_rectangle[theNum].x /( (float)0.5*(g_topright_coordinate.xCoordinate+g_bottomright_coordinate.xCoordinate))) * 0.5*(CO.cornerPoint[0].x+CO.cornerPoint[1].x);
	   	     }
	   	     else
	   	     {
	   	    	 g_rectangle[theNum].x = (g_rectangle[theNum].x / ((float)0.5*(g_bottomleft_coordinate.xCoordinate+g_topleft_coordinate.xCoordinate))) * 0.5*(CO.cornerPoint[2].x+CO.cornerPoint[3].x);
	   	     }

	         if(g_rectangle[theNum].y >= 0)
	         {
	      	    g_rectangle[theNum].y = (g_rectangle[theNum].y /( (float)0.5*(g_topleft_coordinate.yCoordinate+g_topright_coordinate.yCoordinate))) * 0.5*(CO.cornerPoint[0].y+CO.cornerPoint[3].y);
	         }
	         else
	         {
				g_rectangle[theNum].y = (g_rectangle[theNum].y /((float) 0.5*(g_bottomleft_coordinate.yCoordinate+g_bottomright_coordinate.yCoordinate))) * 0.5*(CO.cornerPoint[1].y+CO.cornerPoint[2].y);
	         }
//	         g_rectangle_buff[theNum].y=g_rectangle[theNum].y;
//	         g_rectangle[theNum].y=g_rectangle[theNum].x;
//	         g_rectangle[theNum].x=g_rectangle_buff[theNum].y;
//	   		 HAL_UART_Transmit(&huart2, " g_origin_coordinate.x", 22,200 );
//	   	     sendInt(g_origin_coordinate.xCoordinate);
//	   		 HAL_UART_Transmit(&huart2, " g_origin_coordinate.y", 22,200 );
//	   	     sendInt(g_origin_coordinate.yCoordinate);
//	   		 HAL_UART_Transmit(&huart2, "/n", 2,200 );
//
//	   		 HAL_UART_Transmit(&huart2, " g_topright_coordinate.x", 23,200 );
//	   	     sendInt(g_topright_coordinate.xCoordinate);
//	   		 HAL_UART_Transmit(&huart2, " g_topright_coordinate.y", 23,200 );
//	   	     sendInt(g_topright_coordinate.yCoordinate);
//	   		 HAL_UART_Transmit(&huart2, "/n", 2,200 );
//
	   		 HAL_UART_Transmit(&huart2, " num=", 5,200 );
	   	     sendInt(theNum);
//	   		 HAL_UART_Transmit(&huart2, " x=", 3,200 );
//	   	     sendInt(g_rectangle[theNum].x);
//	   		 HAL_UART_Transmit(&huart2, " y=", 3,200 );
//	   	     sendInt(g_rectangle[theNum].x);
//	   		 HAL_UART_Transmit(&huart2, "/n", 2,200 );
//
//	   		 HAL_UART_Transmit(&huart2, " CO.cornerPoint[0].x=",21 ,200 );
//	   	     sendInt(CO.cornerPoint[0].x);
//	   		 HAL_UART_Transmit(&huart2, " CO.cornerPoint[0].y=",21 ,200 );
//	   	     sendInt(CO.cornerPoint[0].y);
//	   		 HAL_UART_Transmit(&huart2, '/n', 2,200 );
//	   		 HAL_UART_Transmit(&huart2, " CO.cornerPoint[2].x=",21 ,200 );
//	   	     sendInt(CO.cornerPoint[2].x);
//	   		 HAL_UART_Transmit(&huart2, " CO.cornerPoint[2].y=",21 ,200 );
//	   	     sendInt(CO.cornerPoint[2].y);
//	   		 HAL_UART_Transmit(&huart2, "/n", 2,200 );
     }


}



//将接收的红或绿坐标解析

SingleCoordinateDef singleLightCoordinateGet(uint8_t *rxProBuf){


	SingleCoordinateDef theCoordinate;
	 theCoordinate.xCoordinate=Numerical_analysis(rxProBuf,'*',&rxProBuf);

	 theCoordinate.yCoordinate=Numerical_analysis(rxProBuf,'#',&rxProBuf);
//		 HAL_UART_Transmit(&huart2, " x=", 3,200 );
//	     sendInt(theCoordinate.xCoordinate);
//		 HAL_UART_Transmit(&huart2, " y=", 3,200 );
//	     sendInt(theCoordinate.yCoordinate);
//	 if(theCoordinate.xCoordinate==100){
//	   	  HAL_UART_Transmit(&huart2, "x=100", 5,100 );
//	 }
//	 if(theCoordinate.yCoordinate==600){
//	   	  HAL_UART_Transmit(&huart2, "y=600", 5,100 );
//	 }
//      if(theCoordinate.xCoordinate==60){
//         HAL_UART_Transmit(&huart2, "60", 3,20 );
//      }else HAL_UART_Transmit(&huart2, "noon", 4,20 );
   return theCoordinate;
}


//将同时接收的红、绿坐标解析
void doubleLightCoordinateGet(uint8_t *rxProBuf)
{
	g_red_coordinate.xCoordinate=Numerical_analysis(rxProBuf,'*',&rxProBuf);
	g_red_coordinate.yCoordinate=Numerical_analysis(rxProBuf,'*',&rxProBuf);
	g_green_coordinate.xCoordinate=Numerical_analysis(rxProBuf,'*',&rxProBuf);
	g_green_coordinate.yCoordinate=Numerical_analysis(rxProBuf,'#',&rxProBuf);



}


//发送指令，改变树莓派的模式
void changeRaspiMood(uint8_t raspiMood){

	  if(raspiMood==RASRED)
	  {
	   HAL_UART_Transmit(&huart2, "RSM0ST", 6,200 );
	  }
	  else if(raspiMood==RASRECTANGLE)
	  {
	   HAL_UART_Transmit(&huart2, "RSM1ST", 6,200 );
	   HAL_Delay(50);
	   HAL_UART_Transmit(&huart2, "RSABRST", 7,200 );
	  }
	  else if(raspiMood==SETFOVPARAM)
	  {
	   HAL_UART_Transmit(&huart2, "RSM2ST", 6,200 );
	   HAL_Delay(50);
	   HAL_UART_Transmit(&huart2, "RSS0ST", 6,200 );
	  }
	  else if(raspiMood==SETREDPARAM)
	  {
	   HAL_UART_Transmit(&huart2, "RSM2ST", 6,200 );
	   HAL_Delay(50);
	   HAL_UART_Transmit(&huart2, "RSS1ST", 6,200 );
	  }
	  else if(raspiMood==SETRECTPARAM)
	  {
	   HAL_UART_Transmit(&huart2, "RSM2ST", 6,200 );
	   HAL_Delay(50);
	   HAL_UART_Transmit(&huart2, "RSS2ST", 6,200 );
	  }
}

uint8_t getRaspiMood(uint8_t *rxProBuf){
}

void positionCorrection(uint8_t data){
    switch (data) {		   //换算坐标中心
        case 1:
//	   		HAL_UART_Transmit(&huart2, "aaa", 3,200 );
//	   	    sendInt(g_red_coordinate.xCoordinate);
//	   		HAL_UART_Transmit(&huart2, "bbb", 3,200 );
//	   	    sendInt(g_red_coordinate.yCoordinate);
            g_origin_coordinate = g_red_coordinate;
//	   		HAL_UART_Transmit(&huart2, "aaa", 3,200 );
//	   	    sendInt(g_origin_coordinate.xCoordinate);
//	   		HAL_UART_Transmit(&huart2, "bbb", 3,200 );
//	   	    sendInt(g_origin_coordinate.yCoordinate);
            set=0;
            break;
        case 2:
        	g_topright_coordinate = g_red_coordinate;
            g_topright_coordinate.xCoordinate=(g_red_coordinate.xCoordinate-g_origin_coordinate.xCoordinate);
            g_topright_coordinate.yCoordinate=(g_origin_coordinate.yCoordinate-g_red_coordinate.yCoordinate);
            set=0;
            break;
        case 3:
            g_bottomright_coordinate = g_red_coordinate;
            g_bottomright_coordinate.xCoordinate=(g_red_coordinate.xCoordinate-g_origin_coordinate.xCoordinate);
            g_bottomright_coordinate.yCoordinate=(g_origin_coordinate.yCoordinate-g_red_coordinate.yCoordinate);
            set=0;
            break;
        case 4:
            g_bottomleft_coordinate = g_red_coordinate;
            g_bottomleft_coordinate.xCoordinate=(g_red_coordinate.xCoordinate-g_origin_coordinate.xCoordinate);
            g_bottomleft_coordinate.yCoordinate=(g_origin_coordinate.yCoordinate-g_red_coordinate.yCoordinate);
            set=0;
            break;
        case 5:
            g_topleft_coordinate = g_red_coordinate;
            g_topleft_coordinate.xCoordinate=(g_red_coordinate.xCoordinate-g_origin_coordinate.xCoordinate);
            g_topleft_coordinate.yCoordinate=(g_origin_coordinate.yCoordinate-g_red_coordinate.yCoordinate);
            set=0;
            break;
        default:
            // 处理其他情况
            break;
    }




}


PointDef changeToPWM(SingleCoordinateDef data){


	PointDef coordinate;

	return coordinate;
}




